package com.sxp.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sxp.dao.RoleEnum;
import com.sxp.model.Admin;
import com.sxp.model.FoodSelection;
import com.sxp.model.User;
import com.sxp.service.*;

public class UserController extends HttpServlet {
    private final long serialVersionUID = 1L;
    private String LOGIN = "/index.jsp";
    private String FOOD_SELECTION = "/food_selection.jsp";

    private static Service service;

    public UserController() {
        super();
        service = new ServiceImpl();
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("foodSelections")) {
            if(session.getAttribute("username") != null){
                forward = FOOD_SELECTION;
                request.setAttribute("foodselections1", service.getFoodSelections(session.getAttribute("username").toString(),1));
                request.setAttribute("foodselections2", service.getFoodSelections(session.getAttribute("username").toString(),2));
                request.setAttribute("foodselections3", service.getFoodSelections(session.getAttribute("username").toString(),3));
                request.setAttribute("ricetypes",service.getRiceList());
                request.setAttribute("mealtypes",service.getMealList());
            }else{
                forward = LOGIN;
            }
        }
        else if (action.equalsIgnoreCase("logout")){
            session.removeAttribute("username");
            session.removeAttribute("role");
            session.removeAttribute("name");
            forward = LOGIN;
        }else{
            forward = LOGIN;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String forward="";
        String action = request.getParameter("action");

        if (action.equalsIgnoreCase("login")){
            String userName = request.getParameter("username");
            String password = request.getParameter("password");
            User user = new User(userName,password);
            User loginUser = service.authenticateUser(user);
            if (loginUser != null) {
                if(loginUser instanceof Admin){
                    forward = "DailyFoodController?action=toDay";
                    session.setAttribute("username", loginUser.getUserName());
                    session.setAttribute("role", RoleEnum.ADMIN);
                    session.setAttribute("name", loginUser.getName());
                }else {
                    forward = "UserController?action=foodSelections";
                    session.setAttribute("username", loginUser.getUserName());
                    session.setAttribute("role", RoleEnum.USER);
                    session.setAttribute("name", loginUser.getName());
                }
            }
        }else if(action.equalsIgnoreCase("foodSelections")){
            if(session.getAttribute("username") != null){
                forward = FOOD_SELECTION;
                request.setAttribute("foodselections1", service.getFoodSelections(session.getAttribute("username").toString(),1));
                request.setAttribute("foodselections2", service.getFoodSelections(session.getAttribute("username").toString(),2));
                request.setAttribute("foodselections3", service.getFoodSelections(session.getAttribute("username").toString(),3));
                request.setAttribute("ricetypes",service.getRiceList());
                request.setAttribute("mealtypes",service.getMealList());
            }else{
                forward = LOGIN;
            }
        }else if(action.equalsIgnoreCase("saveFoodSelections")){
            if(session.getAttribute("username") != null){
                List<FoodSelection> foodSelections = new ArrayList<FoodSelection>();

                int[] ndays = {
                        Integer.parseInt(request.getParameter("ndays1")),
                        Integer.parseInt(request.getParameter("ndays2")),
                        Integer.parseInt(request.getParameter("ndays3"))
                };

                FoodSelection foodSelections1;
                FoodSelection foodSelections2;
                FoodSelection foodSelections3;

                String userName = session.getAttribute("username").toString();
                String[] dates = new String[3];

                for(int i=1; i<=31; i++){
                    if(i<=ndays[0]){
                        foodSelections1 = new FoodSelection();
                        foodSelections1.setUserName(userName);
                        dates[0] = request.getParameter("date1"+i);
                        String[] dateParts = dates[0].split("/");
                        foodSelections1.setDay(Integer.parseInt(dateParts[0]));
                        foodSelections1.setMonth(Integer.parseInt(dateParts[1]));
                        foodSelections1.setYear(Integer.parseInt(dateParts[2]));
                        foodSelections1.setRiceId(Integer.parseInt(request.getParameter("ricetype1"+i)));
                        foodSelections1.setMealId(Integer.parseInt(request.getParameter("mealtype1"+i)));

                        foodSelections.add(foodSelections1);
                    }
                    if(i<=ndays[1]){
                        foodSelections2 = new FoodSelection();
                        foodSelections2.setUserName(userName);
                        dates[1] = request.getParameter("date2"+i);
                        String[] dateParts = dates[1].split("/");
                        foodSelections2.setDay(Integer.parseInt(dateParts[0]));
                        foodSelections2.setMonth(Integer.parseInt(dateParts[1]));
                        foodSelections2.setYear(Integer.parseInt(dateParts[2]));
                        foodSelections2.setRiceId(Integer.parseInt(request.getParameter("ricetype2"+i)));
                        foodSelections2.setMealId(Integer.parseInt(request.getParameter("mealtype2"+i)));

                        foodSelections.add(foodSelections2);
                    }
                    if(i<=ndays[2]){
                        foodSelections3 = new FoodSelection();
                        foodSelections3.setUserName(userName);
                        dates[2] = request.getParameter("date3"+i);
                        String[] dateParts = dates[2].split("/");
                        foodSelections3.setDay(Integer.parseInt(dateParts[0]));
                        foodSelections3.setMonth(Integer.parseInt(dateParts[1]));
                        foodSelections3.setYear(Integer.parseInt(dateParts[2]));
                        foodSelections3.setRiceId(Integer.parseInt(request.getParameter("ricetype3"+i)));
                        foodSelections3.setMealId(Integer.parseInt(request.getParameter("mealtype3"+i)));

                        foodSelections.add(foodSelections3);
                    }
                }
                service.setFoodSelections(foodSelections);
                forward = "UserController?action=foodSelections";
            }else{
                forward = LOGIN;
            }
            request.setAttribute("foodselections1", service.getFoodSelections(session.getAttribute("username").toString(),1));
            request.setAttribute("foodselections2", service.getFoodSelections(session.getAttribute("username").toString(),2));
            request.setAttribute("foodselections3", service.getFoodSelections(session.getAttribute("username").toString(),3));
            request.setAttribute("ricetypes",service.getRiceList());
            request.setAttribute("mealtypes",service.getMealList());
        }else{
            forward = LOGIN;
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }
}

